package com.cqc.actionbarcompat_listpopupmenu01;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by cui on 2017/3/19.
 */

public class PopupFragment2 extends Fragment implements View.OnClickListener {

    private ArrayList<String> list = new ArrayList<String>();
    private ListView listView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2, container, false);
        listView = (ListView) rootView.findViewById(R.id.listView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        for (int i = 0; i < 100; i++) {
            list.add("item" + i);
        }
        listView.setAdapter(new PopupAdapter(list));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String text = (String) listView.getItemAtPosition(position);
                Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onClick(View view) {
        final PopupAdapter adapter = (PopupAdapter) listView.getAdapter();
        final String text = (String) view.getTag();

        PopupMenu menu = new PopupMenu(getActivity(), view);
        menu.getMenuInflater().inflate(R.menu.menu_popup, menu.getMenu());
        menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.menu1:
                        adapter.remove(text);
                        break;
                }
                return true;//true、false都可以
            }
        });
        menu.show();
    }

    public class PopupAdapter extends ArrayAdapter<String> {
        public PopupAdapter(ArrayList<String> list) {
            super(getActivity(), R.layout.item_list_fragment, R.id.tv1, list);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
            iv_delete.setTag(getItem(position));
            iv_delete.setOnClickListener(PopupFragment2.this);
            return view;
        }
    }
}
